<template>
  <el-breadcrumb separator="/" class="breadcrumb-container">
    <el-breadcrumb-item :to="{ path: '/' }" class="breadcrumb-item">
      <el-icon><HomeFilled /></el-icon>
      <span class="breadcrumb-text">首页</span>
    </el-breadcrumb-item>
    <template v-for="(route, index) in displayRoutes" :key="route.path || index">
      <el-breadcrumb-item
        v-if="index === displayRoutes.length - 1"
        class="breadcrumb-item breadcrumb-item-active"
      >
        <template v-if="route.meta && route.meta.icon">
          <el-icon :size="16"><component :is="route.meta.icon" /></el-icon>
        </template>
        <span class="breadcrumb-text">{{ route.meta?.title || '未命名页面' }}</span>
      </el-breadcrumb-item>
      <el-breadcrumb-item
        v-else
        :to="{ path: generatePath(route) }"
        class="breadcrumb-item"
        @click.stop
      >
        <template v-if="route.meta && route.meta.icon">
          <el-icon :size="16"><component :is="route.meta.icon" /></el-icon>
        </template>
        <span class="breadcrumb-text">{{ route.meta?.title || '未命名页面' }}</span>
      </el-breadcrumb-item>
    </template>
  </el-breadcrumb>
</template>

<script>
import { computed } from 'vue'
import { HomeFilled } from '@element-plus/icons-vue'

export default {
  name: 'Breadcrumb',
  components: {
    HomeFilled
  },
  props: {
    routes: {
      type: Array,
      default: () => []
    }
  },
  setup(props) {
    // 过滤掉不需要显示的路由（如Layout组件）
    const displayRoutes = computed(() => {
      return props.routes.filter(route => {
        // 排除没有name的路由（通常是嵌套的布局路由）
        // 排除带有hiddenBreadcrumb元信息的路由
        return route.name && !route.meta?.hiddenBreadcrumb
      })
    })

    // 生成路由的完整路径
    const generatePath = (route) => {
      // 如果路由有redirect，则使用redirect
      if (route.redirect) {
        return route.redirect
      }
      
      // 构建完整路径
      let path = ''
      // 从根路由开始查找，构建完整路径
      const allRoutes = props.routes
      const targetIndex = allRoutes.findIndex(r => r === route)
      
      if (targetIndex >= 0) {
        for (let i = 0; i <= targetIndex; i++) {
          const r = allRoutes[i]
          if (r.path && r.path !== '/' && r.path.charAt(0) === '/') {
            // 如果是完整路径（以/开头），直接使用
            return r.path
          } else if (r.path) {
            // 否则拼接路径
            path += `/${r.path}`
          }
        }
      }
      
      // 如果没有找到合适的路径，使用当前路由的path
      return route.path || '/' 
    }

    return {
      displayRoutes,
      generatePath
    }
  }
}
</script>

<style scoped>
.breadcrumb-container {
  font-size: 14px;
  height: 40px;
  line-height: 40px;
}

.breadcrumb-item {
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.3s;
}

.breadcrumb-item:hover:not(.breadcrumb-item-active) .breadcrumb-text {
  color: #1890ff;
}

.breadcrumb-item-active {
  cursor: default;
}

.breadcrumb-text {
  color: #606266;
  font-weight: 400;
}

.breadcrumb-item-active .breadcrumb-text {
  color: #303133;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .breadcrumb-container {
    font-size: 12px;
    height: 32px;
    line-height: 32px;
    overflow-x: auto;
    white-space: nowrap;
    padding-right: 10px;
  }
  
  .breadcrumb-container::-webkit-scrollbar {
    height: 4px;
  }
  
  .breadcrumb-container::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 2px;
  }
  
  .breadcrumb-container::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 2px;
  }
  
  .breadcrumb-container::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
  }
  
  .breadcrumb-text {
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
</style>